--[[=======================
            LICENSE
    This file is GPLv3. 
    Praise our lord and saviour RMS
========================= ]]
--[[=======================
            CONSTANS
   ========================= ]]
SONG_TYPE = "stream"
SONG_FILE = "art/jump_and_run_tropics.ogg"
SONG = love.audio.newSource(SONG_FILE, SONG_TYPE)
FONT_SIZE = 40
FONT_FILE = "art/computer_speak_v0.3.ttf"
FONT_COMPUTER = love.graphics.newFont(FONT_FILE, FONT_SIZE)
WINDOW_HEIGHT = 730
WINDOW_WIDTH  = 600
WINDOW_TITLE = "Spykes"

BACKGROUND_IMAGE = "art/jumpGame_ByJanaChumi/assets/bg3.png"
BACKGROUND_X = 0
BACKGROUND_Y = 0

PLATFORM_IMAGE = "art/jumpGame_ByJanaChumi/assets/plat1.png"
SPIKE_IMAGE   = "art/jumpGame_ByJanaChumi/assets/espinho.png"

SCORE_TEXT = "SCORE "
SCORE_COLOR = {0.8627450980392157, 0.6745098039215687, 0.39215686274509803}
SCORE_FONT_SIZE = "SCORE "
SCORE_X = 250
SCORE_Y = 50

LAND = "land"
AIR = "air"
GAMEOVER = "game-over"
BEGIN = "begin"


ESC_KEY = "escape"

function love.load()
    -- LOVE DEFAULTS
    love.audio.play(SONG)
    love.window.setMode(WINDOW_HEIGHT, WINDOW_WIDTH)
    love.window.setTitle(WINDOW_TITLE)
    -- LOVE CONSTANTS
    BACKGROUND_IMG = love.graphics.newImage(BACKGROUND_IMAGE)
    PLATFORM_IMG = love.graphics.newImage(PLATFORM_IMAGE)
    SPIKE_IMG = love.graphics.newImage(SPIKE_IMAGE)
 
    -- LOVE VARIABLES
    platform_num = 4
    gap = 160

    player_status = LAND
    player_position = LEFT
    player = {player_status, player_position}

   
    time = 0
    position = 0
    scroll = 0  -- pixel amount that the platforms are scrolled downward
    bscroll = 0  -- pixel amount background is scrolled downward
    scrolling = false  -- whether the screen is scrolling
    scrollspeed = 320
    spikes = {}
    createSpikes(spikes, platform_num)
    game = {time, position, spikes, player}  -- Deprecated: remove after game_state is implemented
    -- Global state variables
    -- TODO: a lot of the above variables are global and should be moved
    -- into the following
    finite_state = BEGIN
    score = 0
    world_state = {position}
    game_state = {finite_state, score, world_state}
end




--[[=======================
            LOGIC
========================= ]]
-- Produces a random position
-- -> LEFT or RIGHT
function randomPosition()
    local LEFT = 100  -->FIX THIS, should be global?
    local RIGHT = 500 -->FIX THIS, should be global?
    local n = math.random(0,1)
    if n == 0 then
        return LEFT
    else
        return RIGHT
    end
end


-- time deltaTime --> time
-- produces new time
function deltaTime(t, dt)
    local total = t + dt 
    if total > 800 then  --> FIX THIS should be constant, what does 800 mean?
        return 0         --> FIX THIS should be constant, what does 0 mean?
    else
        return total
    end
end

-- spikes_table number -> spikes_table
-- outputs an spikes_table with spikes at random position
function createSpikes(spikes, platform_num)
    for i=0,platform_num do
        spikes[i] = randomPosition()
    end
    return spikes
end

-- table of spikes -> tablo of spikes
-- adds a new spike at the end
function newSpike(spikes, platform)
    local spike = randomPosition()
    spikes[platform] = spike
    return spikes
end


-- table of spikes -> table of spikes
-- removes first spike
function removeSpike(spikes, platform) 
    spikes[platform] = nil
    return spikes
end

-- position delta_time -> position
-- outputs new position for platforms and background
function scrollGame(scroll, dt)
    local PLATHEIGHT = 20
    local scroll = scroll + (scrollspeed*dt)
    local bscroll = bscroll + (scrollspeed*dt)
    local width, height = love.graphics.getDimensions()  --> FIX THS, should this use the GLOBAL variables WIDTH and HEIGHT?
    --if scroll > height then
    --    scroll = -PLATHEIGHT
    --end
    if bscroll > height then
        bscroll = 0
    end
    return scroll, bscroll
end

-- position -> platforms ??
-- what does this function do?
function getPlatforms(scroll)
    local platforms = {}  -- FIX this should be local?? local platforms = {}
    for i=1,platform_num do
        local platformY = scroll+((i-1)*gap)
        table.insert(platforms, platformY)
    end
    return platforms
end

-- spikes index -> spikeX
-- what does this function do?
function getSpike(spikes, index)
    local spikeX = -1000       --> FIX THIS, should be constant? what does -1000 mean?
    local spikeX = spikes[index]
    return spikeX
end

-- delta time -> game
-- creates new game state
function love.update(dt)
    local width, height = love.graphics.getDimensions()
    t = deltaTime(game[1], dt)
    -- print(t)
    spikes = game[3]
    if scrolling then
        scroll, bscroll = scrollGame(scroll, dt)
        local p = getPlatforms(scroll)
        lastplat = p[#p]
        if lastplat > height then  -- TODO: this should be handled in scrollGame() probably
            scrolling = false
            scroll = scroll - gap
            --removeSpike(spikes, #p)
            --newSpike(spikes, 0)
        end
    end
end
--[[=======================
                MOVE PLAYER
========================= ]]

function movePlayer(player)
    if player[1] == LAND then
        getNextPosition(player)
    else
        return(player)
    end
end



--[[=======================
          EVENTS
   ========================= ]]
-- key -> event
-- handles key events
function love.keypressed(key)
    if key == ESC_KEY then  
        love.event.quit()
    end
end

function love.mousepressed(x, y, button)
    --scroll = scroll + 1
    scrolling = true
end








--[[=======================
          DRAW
   ========================= ]]
-- spikes position --> image
-- outputs image of spikes
function drawSpikes(spikes, scroll)
    for i,platformY in ipairs(getPlatforms(scroll)) do
        local spikeX = getSpike(spikes, i)
        local spikeY = platformY - SPIKE_IMG:getHeight()
        love.graphics.draw(SPIKE_IMG, spikeX, spikeY)
    end
end

-- position --> image
-- outputs image of platforms
function drawPlatforms(scroll)
    local LINE = "line"
    local PLATWIDTH = 200
    local PLATHEIGHT = 20
    local PLATLEFT = 50
    local PLATRIGHT = 450
    for i,platformY in ipairs(getPlatforms(scroll)) do
        love.graphics.rectangle(LINE, PLATLEFT, platformY,
            PLATWIDTH, PLATHEIGHT)
        love.graphics.rectangle(LINE, PLATRIGHT, platformY,
            PLATWIDTH, PLATHEIGHT)
    end
end

function drawBackground(scroll)
    local width, height = love.graphics.getDimensions()
    local iwidth, iheight = love.graphics.getDimensions(BACKGROUND_IMG)
    local sx = width/iwidth
    local sy = height/iheight
    local r = 0  -- no rotation
    local scroll1 = scroll-iheight
    local scroll2 = scroll
    love.graphics.draw(BACKGROUND_IMG, BACKGROUND_X, scroll1, r, sx, sy)
    love.graphics.draw(BACKGROUND_IMG, BACKGROUND_X, scroll2, r, sx, sy)
end

function love.draw()
    -- draw game
    drawBackground(bscroll)
    drawPlatforms(scroll)
    drawSpikes(spikes, scroll)
    love.graphics.setFont(FONT_COMPUTER)
    love.graphics.print({SCORE_COLOR, SCORE_TEXT, score}, SCORE_X, SCORE_Y)
end
